﻿using System;
using FluentNHibernate.Mapping;

namespace NaturalKeySpike {
    public abstract class EntityClassMap<T> : ClassMap<T> where T : Entity{
        protected EntityClassMap() {
            Map(x => x.Name, "Name");
            Version(x => x.ModifiedOn).Column("ModifiedOn").CustomType("Timestamp");
        }
    }

    public class TableAMap : EntityClassMap<TableA> {
        public TableAMap() {
            Id(x => x.TableAId, "TableAId").GeneratedBy.Assigned();
            HasMany(x => x.TableBs).KeyColumn("TableAId").Access.CamelCaseField(Prefix.Underscore).Cascade.AllDeleteOrphan().Inverse();
            HasMany(x => x.TableCs).KeyColumn("TableAId").Access.CamelCaseField(Prefix.Underscore).Cascade.AllDeleteOrphan().Inverse();
        }
    }

    public class TableBMap : EntityClassMap<TableB> {
        public TableBMap() {
            CompositeId()
                .KeyReference(x => x.TableA, "TableAId")
                .KeyProperty(x => x.TableBId, "TableBId");
            HasMany(x => x.TableDs).KeyColumns.Add("TableAId", "TableBId").Access.CamelCaseField(Prefix.Underscore).Cascade.AllDeleteOrphan().Inverse();
        }
    }

    public class TableCMap : EntityClassMap<TableC> {
        public TableCMap() {
            CompositeId()
                .KeyReference(x => x.TableA, "TableAId")
                .KeyProperty(x => x.TableCId, "TableCId");
            HasMany(x => x.TableDs).KeyColumns.Add("TableAId", "TableCId").Access.CamelCaseField(Prefix.Underscore).Cascade.AllDeleteOrphan().Inverse();
        }
    }

    public class TableDMap : EntityClassMap<TableD> {
        public TableDMap() {
            CompositeId()
                .KeyReference(x => x.TableB, "TableAId", "TableBId")
                .KeyReference(x => x.TableC, "TableAId", "TableCId")
                .KeyProperty(x => x.TableDId, "TableDId");
        }
    }
}
